﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PFAGame;

namespace MapEditor
{   
    public class TilesTexture
    {
        string basePath = "Images/Themes/";
        string extension = ".png";

        string[] decorPath = new string[] 
        { 
            "Desert/", 
            "Factory/", 
            "Forest/",
            "Labo/"
        };

        string[] tileNames = new string[]
        {
            "Terrain/ground_1",
            "Terrain/road_1",
            "Decors/decor_d_1",
            "Terrain/cliff_north",
            "Decors/decor_d_2",
            "Terrain/cliff_north_east",
            "Terrain/cliff_north_west",
            "Terrain/cliff_south",
            "Terrain/cliff_south_east",
            "Terrain/cliff_south_west",
            "Terrain/cliff_west",
            "Terrain/road_2",
            "Terrain/road_3",
            "Terrain/road_4",
            "Terrain/road_5",
            "Terrain/water",
            "Terrain/bridge_horizontal",
            "Terrain/cliff_east",
            "Decors/decor_e_1",
            "Decors/decor_f_1",
            "Decors/decor_f_2",
            "Decors/decor_g_1",
            "Decors/decor_g_2",
            "Decors/decor_a_2",
            "Decors/decor_a_5",
            "Decors/decor_a_6",
            "Decors/decor_b_2",
            "Decors/decor_b_3",
            "Decors/decor_b_3_alt",
            "Decors/decor_a_1",
            "Decors/decor_a_3",
            "Decors/decor_a_4",          
            "Decors/decor_b_1",
            "Decors/decor_b_4",
            "Decors/decor_b_4_alt",
            "Decors/decor_e_2",
            "Decors/decor_f_3",
            "Decors/decor_f_4",
            "Terrain/cliff_corner_1",
            "Terrain/cliff_corner_2",
            "Side/cube",
            "Side/ground_1",
            "Side/ground_2"
        };

        // decor à utilisé pour l'affichage

        private Decor currentDecor;
        public  Decor CurrentDecor
        {
            get { return currentDecor; }
            set 
            {
                 currentDecor = value;
            }
        }
        private int currentType;
        public int CurrentType
        {
            get { return currentType; }
            set
            {
                    currentType = value;
            }
        }
        //list de texture utilisé pour le rendu
        private List<Texture2D>[] listTexture;

        //variables nécéssaire à l'affichage
        private SpriteBatch spritebatch;
        private GraphicsDevice graphicsDevice;

        //taille d'une tile
        private int height = 64;
        private int width = 64;

        public int Width
        {
            get { return width; }
            set { width = (int)MathHelper.Clamp(value, 16, 128); }
        }

        public int Height
        {
            get { return height; }
            set { height = (int)MathHelper.Clamp(value, 16, 128); }
        }

        public Texture2D this[int n, Decor d]
        {
            set 
            {
                if (currentType == 1)
                    listTexture[(int)d][n] = value;
                else
                    listTexture[(int)d + (int)Decor._SIZE_][n] = value;
            }
            get 
            {
                if (currentType == 1)
                    return listTexture[(int)d][n];
                else
                    return listTexture[(int)d + (int)Decor._SIZE_][n];
            }
        }

        /// <summary>
        /// dessine une tile.
        /// </summary>
        /// <param name="id"> id de la tile a déssiner</param>
        /// <param name="x"> coordonnée X de la tile sur la grid</param>
        /// <param name="y"> coordonnée Y de la tile sur la grid</param>
        public void draw(int id, int x, int y)
        {
            if (id != -1)
            {
                Rectangle r = new Rectangle(x * width, y * height, width, height);
                if (currentType == 1) 
                    spritebatch.Draw(listTexture[(int)CurrentDecor][id], r, Color.White);
                else
                    spritebatch.Draw(listTexture[(int)CurrentDecor + (int)Decor._SIZE_][id], r, Color.White);
            }
        }

        /// <summary>
        /// dessine une tile (surcharge pour l'onglet de collision).
        /// </summary>
        /// <param name="id"> id de la tile a déssiner</param>
        /// <param name="x"> coordonnée X de la tile sur la grid</param>
        /// <param name="y"> coordonnée Y de la tile sur la grid</param>
        /// <param name="collideMapId"> id de collision de la case</param>
        public void draw(int id, int x, int y, int collideMapId)
        {
            if (id != -1)
            {
                Rectangle r = new Rectangle(x * width, y * height, width, height);
                spritebatch.Draw(listTexture[(int)CurrentDecor][id], r, selectColor(collideMapId));
            }
        }

        /// <summary>
        /// renvoie une couleur en fonciton d'un id de collision
        /// </summary>
        private Color selectColor(int collideMapId)
        {
            switch (collideMapId)
            {
                case 0: return Color.White;         // case traverssable
                case 1: return Color.Red;           // case bloquante
                case 2: return Color.Blue;          // spawn du joueur
                case 3: return Color.DarkOrange;    // spawn de mob
                case 4: return Color.Yellow;        // trigger game over
                case 5: return Color.Tomato;        // spawn de boss
                case 6: return Color.DarkGray;      // spawn de reine
                case 7: return Color.DarkGray;      // spawn de chevalier
                case 8: return Color.DarkGray;      // spawn de fou
                case 9: return Color.DarkGray;      // spawn de tour
                                                    // TODO: completer la liste d'ID collision
            }
            return Color.White;
        }
    }
}
